package 越长越合法

func countSubarrays(nums []int, k int) int64 {
	m := 0
	for _, num := range nums {
		m = max(m, num)
	}
	left := 0
	s := 0
	ans := 0
	for right, num := range nums {
		if num == m {
			s++
		}
		for s >= k && left <= right {
			if nums[left] == m {
				s--
			}
			left++
		}
		ans += left
	}
	return int64(ans)
}
